package com.example.mqttclient;

import android.app.Activity;
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class MyApplication extends Application {
    public static final String ALARM_CHANNEL_ID = "alarm_channel";
    private static final String PREFS_NAME = "AppPrefs";
    private static final String KEY_THEME = "theme";
    private int currentTheme;
    private List<Activity> activities = new ArrayList<>();

    public static final String WEATHER_CHANNEL_ID = "weather_channel";

    @Override
    public void onCreate() {
        super.onCreate();
        createNotificationChannel();

        // 初始化当前主题
        SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        currentTheme = prefs.getInt(KEY_THEME, R.style.AppTheme_Light);

        // 注册Activity生命周期回调
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                // 应用当前主题
                activity.setTheme(currentTheme);

                // 注册Activity以便主题变更时通知
                activities.add(activity);
            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {

            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {

            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {

            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                // 从列表中移除Activity
                activities.remove(activity);
            }

        });
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "Alarm Channel";
            String description = "Channel for gas alarm notifications";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel channel = new NotificationChannel(ALARM_CHANNEL_ID, name, importance);
            channel.setDescription(description);

            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "Weather Channel";
            String description = "Channel for weather notifications";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(WEATHER_CHANNEL_ID, name, importance);
            channel.setDescription(description);

            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

    // 获取当前主题
    public int getCurrentTheme() {
        return currentTheme;
    }

    // 设置新主题并通知所有Activity
    public void setTheme(int theme) {
        currentTheme = theme;

        // 保存主题设置
        SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit();
        editor.putInt(KEY_THEME, theme);
        editor.apply();

        // 通知所有已创建的Activity更新主题
        for (Activity activity : activities) {
            updateActivityTheme(activity);
        }
    }

    // 动态更新Activity的主题
    private void updateActivityTheme(Activity activity) {
        View rootView = activity.getWindow().getDecorView().getRootView();
        if (currentTheme == R.style.AppTheme_Light) {
            // 设置亮色主题的视图样式
            rootView.setBackgroundColor(activity.getResources().getColor(android.R.color.white));

            // 更新标题栏文字颜色
            if (activity instanceof AppCompatActivity) {
                AppCompatActivity appCompatActivity = (AppCompatActivity) activity;
                ActionBar actionBar = appCompatActivity.getSupportActionBar();
                if (actionBar != null) {
                    // 设置亮色主题下标题栏文字颜色
                    int titleColor = activity.getResources().getColor(R.color.colorPrimaryLight);
                    int actionBarColor = activity.getResources().getColor(R.color.colorPrimaryLight);
                    actionBar.setBackgroundDrawable(new android.graphics.drawable.ColorDrawable(actionBarColor));
                    // 设置标题文字颜色
                    int titleTextViewId = activity.getResources().getIdentifier("action_bar_title", "id", "android");
                    TextView titleTextView = activity.findViewById(titleTextViewId);
                    if (titleTextView != null) {
                        titleTextView.setTextColor(titleColor);
                    }
                }
            }
        } else {
            // 设置暗色主题的视图样式
            rootView.setBackgroundColor(activity.getResources().getColor(android.R.color.black));

            // 更新标题栏文字颜色
            if (activity instanceof AppCompatActivity) {
                AppCompatActivity appCompatActivity = (AppCompatActivity) activity;
                ActionBar actionBar = appCompatActivity.getSupportActionBar();
                if (actionBar != null) {
                    // 设置暗色主题下标题栏文字颜色
                    int titleColor = activity.getResources().getColor(R.color.colorPrimary_Dark);
                    int actionBarColor = activity.getResources().getColor(R.color.colorPrimary_Dark);
                    actionBar.setBackgroundDrawable(new android.graphics.drawable.ColorDrawable(actionBarColor));
                    // 设置标题文字颜色
                    int titleTextViewId = activity.getResources().getIdentifier("action_bar_title", "id", "android");
                    TextView titleTextView = activity.findViewById(titleTextViewId);
                    if (titleTextView != null) {
                        titleTextView.setTextColor(titleColor);
                    }
                }
            }
        }
    }

    // 获取MyApplication实例的静态方法
    public static MyApplication getInstance(Context context) {
        return (MyApplication) context.getApplicationContext();
    }
}